<template>
  <div class="home-address-wrap" >
        <van-nav-bar
		  title="收货地址"
		  left-arrow
		  fixed
		  @click-left="onClickLeft"
		/>

        <div v-if="finished && !list.length" class="empty-good" >
            <van-empty description="暂无收货地址" />
        </div>

        <div class="address-ul" >
            <div 
            v-for="(item, index) in list"
            :key="index"
            @click="handleToSelect(item)"
            class="address-li" >
                <div class="address-content-name" >
                    <div class="address-user-name" >{{ item.name }}</div>
                    <div class="address-user-tel" >{{ item.phone }}</div>
                </div>
                <div class="address-content-info" >{{ item.full_address }}</div>
                <div class="address-bottom" >
                    <div 
                    class="default-tag" >
                        <img  class="default-tag-icon" :src="item.default_flag == 1 ? defaultTagIcon : defaultCircle" alt="">
                        <div  class="default-tag-name" >默认地址</div>
                    </div>
                    <div class="default-operation" >
                        <div @click.stop="handleToEdit(item.id)" class="default-operation-edit" >编辑</div>
                        <div @click.stop="handleToDel(item.id)"  class="default-operation-del" >删除</div>
                    </div>
                </div>
            </div>
        </div>

        <div @click="handleToAdd" class="add-btn active-btn" >添加收货地址</div>


        <van-popup v-model="showModel" round >
			<div class="popup-box" >
				<div class="popup-box-title" >确认删除该地址？</div>
				<div class="popup-btn" >
					<div @click="showModel = false" class="popup-btn-cancel active-btn" >取消</div>
					<div @click="handleToClear" class="popup-btn-confirm active-btn" >确定</div>
				</div>
			</div>
		</van-popup>


  </div>
</template>

<script>
import { ADDRESS_LIST, ADDRESS_DEL } from '@/api/home';
import { defaultTagIcon, defaultCircle } from '@/tool/static-img';
export default {
    data() {
        return {
            defaultTagIcon, defaultCircle,
            list: [],
            finished: false,
            showModel: false,
            addressId: ''
        }
    },
    created() {
        // 地址列表
        this.getAddressList()
    },
    methods: {
        onClickLeft() {
            this.$router.back()
        },
        // 地址列表
        getAddressList() {
            this.$http.get(ADDRESS_LIST)
                .then(res => {
                    const {
                        data
                    } = res;

                    this.list = data.list || [];
                    this.finished = true;
                })
        },
        // 地址添加
        handleToAdd() {
            this.$router.push({
                name: 'addressEdit'
            })
        },
        handleToDel(id) {
            this.addressId = id;
            this.showModel = true;
        },
        // 地址删除
        handleToClear() {
            this.showModel = false;
            this.$http.post(ADDRESS_DEL + this.addressId)
                .then(() => {
                    this.$toast.success('删除成功');
                    // 地址列表
                    this.getAddressList();
                })
        },
        // 地址编辑
        handleToEdit(id) {
            this.$router.push({
                name: 'addressEdit',
                query: {
                    id
                }
            })
        },
        // 选择地址
        handleToSelect(item) {
            localStorage.setItem('hasSelectAddress', JSON.stringify(item));
            this.$router.back();
        }
    }

}

</script>
<style lang="scss" >
.home-address-wrap{
    position: relative;
    min-height: 100vh;
    padding-top: 62px;
    background-color: #f4f4f4;
    padding-bottom: calc(1.2rem + constant(safe-area-inset-bottom));
    padding-bottom: calc(1.2rem + env(safe-area-inset-bottom));
    box-sizing: border-box;
    .van-nav-bar .van-icon{
        color: #333333;
    }
    .van-nav-bar__arrow{
        font-size: 20px;
    }
    .van-hairline--bottom::after{
        display: none;
    }

    .add-btn{
        position: fixed;
        left: 0;
        right: 0;
        bottom: 0.24rem;
        margin: auto;
        display: flex;
        align-items: center;
        justify-content: center;
        width: 6.86rem;
        height: 0.88rem;
        font-size: 0.32rem;
        font-weight: 500;
        color: #ffffff;
        border-radius: 0.16rem;
        background: linear-gradient( 270deg, #FD6D17 0%, #FF5E40 100%);
    }

    .empty-good{
        padding-top: 1rem;
    }
    .address-ul{
        .address-li{
            width: 6.86rem;
            margin: auto;
            margin-bottom: 0.24rem;
            padding: 0.32rem;
            background: #ffffff;
            border-radius: 0.16rem;
            box-sizing: border-box;
            .address-content-name{
                display: flex;
                align-items: center;
                margin-bottom: 0.16rem;
                .address-user-name{
                    height: 0.44rem;
                    margin-right: 0.16rem;
                    line-height: 0.44rem;
                    font-weight: 500;
                    font-size: 0.32rem;
                }
                .address-user-tel{
                    font-size: 0.28rem;
                    font-weight: 400;
                    color: #999999;
                }
            }
            .address-content-info{
                margin-bottom: 0.32rem;
                font-size: 0.28rem;
                font-weight: 400;
                line-height: 0.4rem;
                color: #666666;
            }
            .address-bottom{
                display: flex;
                align-items: center;
                justify-content: space-between;
                .default-tag{
                    display: flex;
                    align-items: center;
                    .default-tag-icon{
                        width: 0.32rem;
                        height: 0.32rem;
                        margin-right: 0.16rem;
                    }
                    .default-tag-name{
                        height: 0.4rem;
                        line-height: 0.4rem;
                        font-weight: 400;
                        font-size: 0.28rem;
                        color: #999999;
                    }
                }
                .default-operation{
                    display: flex;
                    align-items: center;
                    font-weight: 400;
                    font-size: 0.28rem;
                        color: #999999;
                    .default-operation-edit{
                        margin-right: 0.32rem;
                    }
                    .default-operation-del{
                    }
                }
            }
        }

        
    }

    .popup-box{
			width: 5.74rem;
			height: 3.08rem;
			padding-top: 0.72rem;
			box-sizing: border-box;
			background-color: #fff;
			border-radius: 0.16rem;
			.popup-box-title{
				height: 0.44rem;
				margin-bottom: 0.72rem;
				line-height: 0.44rem;
				font-size: 0.32rem;
				font-weight: 400;
				color: #333333;
				text-align: center;
			}
			.popup-btn{
				display: flex;
				justify-content: space-between;
				padding: 0 0.32rem;
				.popup-btn-cancel{
					display: flex;
					align-items: center;
					justify-content: center;
					width: 2.4rem;
					height: 0.8rem;
					font-weight: 400;
					font-size: 0.32rem;
					color: #333333;
					border-radius: 0.16rem;
					border: 0.02rem solid #E3E3E3;
					box-sizing: border-box;
				}
				.popup-btn-confirm{
					display: flex;
					align-items: center;
					justify-content: center;
					width: 2.4rem;
					height: 0.8rem;
					font-size: 0.32rem;
					font-weight: 400;
					color: #fff;
					background: linear-gradient( 270deg, #FD6D17 0%, #FF5E40 100%);
					border-radius: 0.16rem;
				}
			}
			
		}
		
		.van-popup--center.van-popup--round{
			border-radius: 0.16rem;
		}
}
</style>